package com.example.seh.util;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.seh.R;
import com.example.seh.constant.Constant;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class ForgetPasswordActivity extends AppCompatActivity {
    private TextView getCode;
    private EditText account, password, code, confirm;
    private Button submit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_password);
        getViews();
        setListeners();
    }

    private void setListeners() {
        getCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ("".equals(account.getText().toString())) {
                    Toast.makeText(getApplicationContext(), "请填写手机号", Toast.LENGTH_SHORT).show();
                } else {
                    if (judgePhone(account.getText().toString())) {
                        getCode();
                    } else {
                        Toast.makeText(getApplicationContext(), "手机号不正确", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!"".equals(account.getText().toString()) && !"".equals(password.getText().toString()) && !"".equals(confirm.getText().toString()) && !"".equals(code.getText().toString())) {
                    if (confirm.getText().toString().equals(password.getText().toString())) {
                        msgVerify();
                    } else {
                        Toast.makeText(getApplicationContext(), "两次密码不一致", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(getApplicationContext(), "请填写完整信息", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
    private void msgVerify() {
            FormBody body = new FormBody.Builder()
                    .add("phone", account.getText().toString())
                    .add("password", password.getText().toString())
                    .add("code", code.getText().toString())
                    .build();
            OkHttpClient okHttpClient = new OkHttpClient();
            Request request = new Request.Builder()
                    .post(body)
                    .url(Constant.FORGETPASSWORD)
                    .build();
            Log.e("地址：", request.url().toString());
            Call call = okHttpClient.newCall(request);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    Log.e("okhttp", "获取数据失败");
                    e.printStackTrace();
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    String result = response.body().string();
                    Log.e("for",result);
                    if ("修改成功".equals(result)) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(getApplicationContext(), "修改成功", Toast.LENGTH_SHORT).show();
                            }
                        });
                        finish();
                    } else {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(getApplicationContext(), "修改失败", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                }
            });
        }

    private void getCode() {
        Log.e("getcode", account.getText().toString());
        FormBody body = new FormBody.Builder()
                .add("phone", account.getText().toString())
                .build();
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .post(body)
                .url(Constant.GETCODE)
                .build();
        Log.e("地址：", request.url().toString());
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(ForgetPasswordActivity.this, "发送失败", Toast.LENGTH_SHORT).show();
                    }
                });
                Log.e("okhttp", "获取数据失败");
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String isLogin = jsonObject.getString("data");
                    Log.e("LoginData", isLogin);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private boolean judgePhone(String phoneNum) {
        boolean mobileNO = PhoneUtil.isMobileNO(phoneNum);
        return mobileNO;
    }
    private void getViews() {
        account = findViewById(R.id.forget_password_account);
        password = findViewById(R.id.forget_password_new_password);
        confirm = findViewById(R.id.forget_password_confirm);
        code = findViewById(R.id.forget_password_code);
        getCode = findViewById(R.id.forget_password_get_code);
        submit = findViewById(R.id.forget_password_submit);
    }
}